package com.jboost.modules.app.service.strategry.hx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.dto.HxProductDto;
import com.jboost.modules.app.dto.huixin.HxProductDetailDto;
import com.jboost.modules.app.service.strategry.common.AbstractJdy;
import com.jboost.modules.system.entity.FhdBom;
import com.jboost.modules.system.entity.FhdProductDz;
import com.jboost.modules.system.entity.HxProduct;
import com.jboost.modules.system.mapper.FhdProductMapper;
import com.jboost.modules.system.mapper.HxProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public abstract class AbstractHxPoduct<T, V> extends AbstractJdy<T,V, HxProductMapper, HxProduct>{

    @Autowired
    StringRedisTemplate redisTemplate;

    public String getAppId() {
        return "648413a12b740b0008c630a7";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "smLHulipnrxt6Lx9x1GqA0CyzpSl4k2o");
        return jdyHeaders;
    }

   @Override
    public void sync(HxProduct entit) throws IOException {
        log.info("开始批量查询Hs材料数据");
        HashMap<String, Object> param = getParam();
        param.put("limit",1000);
        String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
        List<JSONObject> jsonObjects = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
        jsonObjects.forEach(e->{
            HxProductDto hxProductDto = JSONObject.parseObject(e.toString(), HxProductDto.class);
            HxProduct hxProduct = baseMapper.selectById(hxProductDto.getId());
            if (Objects.isNull(hxProduct)){
                HxProduct hxProduct1 = new HxProduct();
                hxProduct1.setId(hxProductDto.getId());
                hxProduct1.setHxxh(hxProductDto.getHxxh());
                hxProduct1.setDzjt(JSONArray.toJSONString(hxProductDto.getDzjt()));
                hxProduct1.setDztmsyt(JSONArray.toJSONString(hxProductDto.getDztmsyt()));
                hxProduct1.setBzsyt(JSONArray.toJSONString(hxProductDto.getDzjt()));
                hxProduct1.setZsmsyt(JSONArray.toJSONString(hxProductDto.getZsmsyt()));
                hxProduct1.setType(entit.getType());
                baseMapper.insert(hxProduct1);
            }

        });
    }

}
